package com.gupao.edu.homework.chatroom;

import com.gupao.edu.utils.CloseUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @description：
 * @author：huabin.lin@xhmh.com
 * @version：2018年11月04日 20:30
 */
public class ClientReceive implements Runnable {

    /***
     * 输入流
     */
    private BufferedReader in;
    /***
     * 线程运行标识
     */
    private boolean isRunning = Boolean.TRUE;

    public ClientReceive() {
        super();
    }


    public ClientReceive(Socket client) {
        this();
        try {
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
            this.isRunning = false;
            CloseUtil.clearResources(this.in);
        }
    }

    /***
     * 接收服务端传输的数据
     * @return
     */
    private String receive() {
        String msg = "";
        try {
            msg = in.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            this.isRunning = false;
            CloseUtil.clearResources(this.in);
        }
        return msg;
    }

    @Override
    public void run() {
        while (this.isRunning) {
            System.out.println(this.receive());
        }
    }
}
